import logging
from fastapi import APIRouter, Query
from tortoise.expressions import Q

from app.controllers import category_controller
from app.schemas.base import SuccessExtra

logger = logging.getLogger(__name__)
router = APIRouter()


@router.get("/list", summary="类别列表")
async def list_category(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        name: str = Query("", description="类别名称，用于查询")
):
    q = Q()
    if name:
        q = Q(name__contains=name)
    total, category_objs = await category_controller.list(page=page, page_size=page_size, search=q, order=["order"])
    data = [await obj.to_dict() for obj in category_objs]
    return SuccessExtra(data=data, message='success', total=total, page=page, page_size=page_size)
